(function(){
	Ext.define('AppView.base.BaseGrid',{
		extend:'Ext.grid.Panel',
		alias:'widget.basegrid',
		selType: 'checkboxmodel',
		editWin:'',
		gridStore:'',
		moduleUrl:'',//controller
		gridTbar:[],//grid自定义按钮
		btConfig:{//默认按钮
			add:true,
			edit:true,
			del:true,
			refresh:true
		},
		tbar:[
	    	{
	    		text:'添加',
	    		name:'add',
	    		icon:'resources/button/add.png',
	    		handler:function(){
	    			var grid = this.up('grid');
	    			if(grid.editWin){
	    				var formWin = Ext.create(this.up('grid').editWin);
	    				formWin.parentComp = grid;
	    				formWin.show();
	    			}
	    		}
	    	},
	    	{
	    		text:'编辑',
	    		name:'edit',
	    		icon:'resources/button/edit.png',
	    		handler:function(){
	    			var grid = this.up('grid');
					var selection = grid.getSelectionModel().getSelection();
					if(selection.length==0 || selection.length>1){
						Ext.Msg.alert('提示','请选择一条记录进行编辑!');
						return;
					}
	    			var selRecord = selection[0];
	    			if(grid.editWin){
	    				var formWin = Ext.create(this.up('grid').editWin);
	    				formWin.parentComp = grid;
	    				if(!formWin.viewUrl){
	    					Ext.Msg.alert('提示','请设置viewUrl!');
	    					return;
	    				}
	    				formWin.show();
	    				var targetForm = formWin.down('form');
	    				var childForm = targetForm.getForm();
	    				var loadParams={
	    					url:formWin.viewUrl,
	    					method:'post',
	    					params:{id:selRecord.raw.id},
	    					success:function(form,action){
	    						targetForm.handleLoadSuccess(action.result);
	    					},
							failure:function(form,action){
								Ext.Msg.alert('提示',"失败原因是: "+action.result.message);
							}
	    				}
	    				childForm.load(loadParams);
	    			}
	    		}
	    	},
    		{
	    		text:'删除',
	    		name:'del',
	    		icon:'resources/button/del.png',
	    		handler:function(){
	    			var grid = this.up('grid');
					var selection = grid.getSelectionModel().getSelection();
					if(selection.length==0){
						Ext.Msg.alert('提示','请选择记录进行删除!');
						return;
					}
					var ids = [];
					Ext.each(selection,function(data){
						ids.push(data.raw.id)
					});
					if(!grid.moduleUrl){
						Ext.Msg.alert('提示','请设置moduleUrl!');
					};
					Ext.Msg.confirm('提示','确定要删除选中的记录吗?',function(optional){
						if(optional=='yes'){
							Ext.Ajax.request({
								url : grid.moduleUrl+'/del.do',
								params : {
									ids : ids
								},
								success : function(response) {
									var text = Ext.decode(response.responseText);
									Ext.Msg.alert('提示',text.message);
									grid.getStore().reload();
								},
								failure : function(response) {
									var text = Ext.decode(response.responseText);
									Ext.Msg.alert('提示',text.data.message);
								}
							});
						}else{
						
						}
					})
	    		}
    		},
    		{
	    		text:'刷新',
	    		name:'refresh',
	    		icon:'resources/button/refresh.png',
	    		handler:function(){
	    			var grid = this.up('grid');
	    			grid.getStore().reload();
	    		}
    		}
	    ],
	    initComponent:function(){
	    	var gridStore = Ext.create(this.gridStore);
	    	this.store = gridStore;
	    	
		    this.tbar = this.tbar.concat(this.gridTbar);
		    
		    
		    if(!this.dockedItems) {
		    	this.dockedItems = [];
		    	
		    }else{
		    	//设置底部分页
		    	var pagingtoolbar = {
			        xtype: 'pagingtoolbar',
			        store: gridStore,   // same store GridPanel is using
			        dock: 'bottom',
			        displayInfo: true
			    };
			    var isExistsPaging = false;//是否存在分页
			    var existsPagingIndex = 0;//分页index
			    
			    for(i=0;i<this.dockedItems.length;i++){
			    	var date = this.dockedItems[i];
			    	if(date.xtype=='pagingtoolbar'){
			    		isExistsPaging = true;
			    		existsPagingIndex=i;
			    	}
			    }
			    if(!isExistsPaging && !existsPagingIndex){
			    	this.dockedItems.push(pagingtoolbar);
			    }else{
			    	this.dockedItems[existsPagingIndex] = pagingtoolbar;
			    }
		    }
		    
		    this.callParent(arguments);
		    
		    var btConfig = this.btConfig;
		    if(!btConfig.add){
		    	this.down('button[name=add]').hide();
		    }
		    if(!btConfig.edit){
		    	this.down('button[name=edit]').hide();
		    }
		    if(!btConfig.del){
		    	this.down('button[name=del]').hide();
		    }
		    if(!btConfig.refresh){
		    	this.down('button[name=refresh]').hide();
		    }
		    
	    }
	});
})()
